package one.stand.controller.index;

import com.jmp.api.dto.TemplateDiyRequest;
import com.jmp.api.dto.TemplateDiyResponse;
import com.jmp.api.dto.TemplateListResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.*;
import com.jmp.feign.TemplateFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.advice.ResultEnum;
import one.stand.dto.ConvertUtils;
import one.stand.model.ResultModel;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "海报制作")
@RestController
@RequestMapping("template")
public class TemplateController {
    @Autowired
    private TemplateFeign templateFeign;

    @ApiOperation("节日海报列表")
    @PostMapping("list")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<TemplateListResponse> list() {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        final Integer companyId = optUser.getCompanyId();

        TemplateListDto templateListDto = TemplateListDto.builder()
                .companyId(companyId)
                .cardId(optUser.getCardId())
                .build();
        templateListDto.setPageInit(1, 10);
        ResultModel<TemplateListVo> templates = templateFeign.list(templateListDto);
        AssertUtil.isTrue(templates);
        List<TemplateListResponse.Template> templateList = ConvertUtils.list(templates.getData().getCardPage().getList(), template -> {
            return TemplateListResponse.Template.builder()
                    .id(template.getId())
                    .imgUrl(template.getCardUrl())
                    .imgUrlWidth(template.getImgUrlWidth())
                    .imgUrlHeight(template.getImgUrlHeight())
                    .imgWidth(template.getImgWidth())
                    .imgHeight(template.getImgHeight())
                    .positionWidth(template.getPositionWidth())
                    .positionHeight(template.getPositionHeight())
                    .build();
        });

        //名片海报
//        BusinessCardRequest cardRequest = new BusinessCardRequest();
//        cardRequest.setCardId(optUser.getCardId());
//        cardRequest.setId(Long.valueOf(optUser.getUserId()));
//        ResultModel<List<String>> card3 = crmFegin.getCardPoster(cardRequest);
//        if (card3.isSuccess()) {
//            List<TemplateListResponse.Template> card3List = ConvertUtils.list(card3.getData(), String -> TemplateListResponse.Template.builder()
//                    .imgUrl(String).build());
//            templateList.addAll(card3List);
//        }

        TemplateListResponse response = TemplateListResponse.builder()
                .templates(templateList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("保存自定义")
    @PostMapping("diy")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<TemplateDiyResponse> saveDiy(@RequestAttribute TemplateDiyRequest request) {
        AssertUtil.nonNull(request.getDiyUrl(), ResultEnum.PARAM_CHECK);

        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        TemplateCreateRequest createRequest = TemplateCreateRequest.builder()
                .templateId(request.getTemplateId())
                .cardId(optUser.getCardId())
                .diyUrl(request.getDiyUrl())
                .build();
        ResultModel<TemplateCreateResponse> createResponseResultModel = templateFeign.create(createRequest);
        AssertUtil.isTrue(createResponseResultModel);
        String cardUrl = createResponseResultModel.getData().getImageUrl();

        TemplateDiyResponse response = TemplateDiyResponse.builder()
                .cardUrl(cardUrl)
                .build();

        return ResultModel.success(response);
    }
}
